/**
 * 
 */
package tests;

import static org.junit.Assert.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import network.Address;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import coreAlgorithm.ID;
import coreAlgorithm.NodeTriple;
import coreAlgorithm.command.FindValueResponse;

/**
 * @author Pentius
 * 
 */
public class FindValueResponseTest {
	static List<NodeTriple> proposedList;
	static Serializable proposedValue;
	FindValueResponse valueResp;
	FindValueResponse listResp;

	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		valueResp = new FindValueResponse(proposedValue);
		listResp = new FindValueResponse(proposedList);
	}

	@BeforeClass
	public static void setUpBeforeClass() {
		proposedList = new ArrayList<NodeTriple>();
		for (int i = 0; i < 10; i++) {
			proposedList.add(new NodeTriple(new Address("Address " + i), new ID(i), i));
		}
		proposedValue = "that's the value!";
	}

	@Test(expected = RuntimeException.class)
	public void testEmptyCloning() throws Exception {
		FindValueResponse testResp = new FindValueResponse((Serializable) null);
		testResp.clone();
	}

	@Test
	public void testArrayListCloning() throws Exception {
		FindValueResponse newResp = (FindValueResponse) listResp.clone();
		List<NodeTriple> clonedList = newResp.getResponseList(); 
		Iterator<NodeTriple> originalIter = proposedList.iterator();
		for (Iterator<NodeTriple> clonedIter = clonedList.iterator(); clonedIter.hasNext();) {
			NodeTriple clonedNT = (NodeTriple) clonedIter.next();
			NodeTriple originalNT = (NodeTriple) originalIter.next();
			assertTrue(clonedNT != originalNT);
			assertEquals(originalNT, clonedNT);			
		}
	}
	
	@Test
	public void testSerializableCloning() throws Exception {
		FindValueResponse newResp = (FindValueResponse) valueResp.clone();
		assertTrue(newResp.getValue() != valueResp.getValue());
		assertTrue(((String) newResp.getValue()).equals(valueResp.getValue()));
	}
}
